package com.sscl.baselibrary.exts

import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import com.sscl.baselibrary.R

/**
 * ContextExt
 *
 * Context 扩展类
 * @author jackie Penghe
 * @date 2024/9/4
 */


/**
 * 跳转到系统设置界面
 */
fun Context.toSystemSetting(
    showBackBtn: Boolean = true,
    backText: String = getString(R.string.com_jackiepenghe_back),
    showNextBtn: Boolean = false,
    nextBtnText: String = getString(R.string.com_jackiepenghe_finish)
) {
    val intent = Intent(Settings.ACTION_SETTINGS)
    intent.putExtra("extra_prefs_show_button_bar", showBackBtn)
    intent.putExtra("extra_prefs_set_back_text", backText)
    if (showNextBtn) {
        intent.putExtra("extra_prefs_set_next_text", nextBtnText)
    }
    startActivity(intent)
}

/**
 * 跳转到WIFI设置界面
 */
fun Context.toWifiSetting(
    showBackBtn: Boolean = true,
    backText: String = getString(R.string.com_jackiepenghe_back),
    showNextBtn: Boolean = true,
    nextBtnText: String = getString(R.string.com_jackiepenghe_finish)
) {
    val intent = Intent(Settings.ACTION_WIFI_SETTINGS)
    intent.putExtra("extra_prefs_show_button_bar", showBackBtn)
    intent.putExtra("extra_prefs_set_back_text", backText)
    if (showNextBtn) {
        intent.putExtra("extra_prefs_set_next_text", nextBtnText)
    }
    startActivity(intent)
}

/**
 * 跳转到语音控制飞行模式设置界面
 */
@RequiresApi(Build.VERSION_CODES.M)
fun Context.toVoiceControlAirplaneModeSetting(
    showBackBtn: Boolean = true,
    backText: String = getString(R.string.com_jackiepenghe_back),
    showNextBtn: Boolean = true,
    nextBtnText: String = getString(R.string.com_jackiepenghe_finish)
) {
    val intent = Intent(Settings.ACTION_VOICE_CONTROL_AIRPLANE_MODE)
    intent.putExtra("extra_prefs_show_button_bar", showBackBtn)
    intent.putExtra("extra_prefs_set_back_text", backText)
    if (showNextBtn) {
        intent.putExtra("extra_prefs_set_next_text", nextBtnText)
    }
    startActivity(intent)
}

/**
 *  跳转到飞行模式设置界面(部分手机会跳转到更多连接设置)
 */
fun Context.toAirplaneModeSetting(
    showBackBtn: Boolean = true,
    backText: String = getString(R.string.com_jackiepenghe_back),
    showNextBtn: Boolean = false,
    nextBtnText: String = getString(R.string.com_jackiepenghe_finish)
) {
    val intent = Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS)
    intent.putExtra("extra_prefs_show_button_bar", showBackBtn)
    intent.putExtra("extra_prefs_set_back_text", backText)
    if (showNextBtn) {
        intent.putExtra("extra_prefs_set_next_text", nextBtnText)
    }
    startActivity(intent)
}

/**
 *  跳转到飞行模式设置界面
 */
fun Context.toDisplaySetting(
    showBackBtn: Boolean = true,
    backText: String = getString(R.string.com_jackiepenghe_back),
    showNextBtn: Boolean = false,
    nextBtnText: String = getString(R.string.com_jackiepenghe_finish)
) {
    val intent = Intent(Settings.ACTION_DISPLAY_SETTINGS)
    intent.putExtra("extra_prefs_show_button_bar", showBackBtn)
    intent.putExtra("extra_prefs_set_back_text", backText)
    if (showNextBtn) {
        intent.putExtra("extra_prefs_set_next_text", nextBtnText)
    }
    startActivity(intent)
}

/**
 *  跳转到时间日期设置界面
 */
fun Context.toDateSetting(
    showBackBtn: Boolean = true,
    backText: String = getString(R.string.com_jackiepenghe_back),
    showNextBtn: Boolean = false,
    nextBtnText: String = getString(R.string.com_jackiepenghe_finish)
) {
    val intent = Intent(Settings.ACTION_DATE_SETTINGS)
    intent.putExtra("extra_prefs_show_button_bar", showBackBtn)
    intent.putExtra("extra_prefs_set_back_text", backText)
    if (showNextBtn) {
        intent.putExtra("extra_prefs_set_next_text", nextBtnText)
    }
    startActivity(intent)
}

/**
 * 跳转到辅助功能设置界面
 */
fun Context.toAccessibilitySetting(
    showBackBtn: Boolean = true,
    backText: String = getString(R.string.com_jackiepenghe_back),
    showNextBtn: Boolean = false,
    nextBtnText: String = getString(R.string.com_jackiepenghe_finish)
) {
    val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
    intent.putExtra("extra_prefs_show_button_bar", showBackBtn)
    intent.putExtra("extra_prefs_set_back_text", backText)
    if (showNextBtn) {
        intent.putExtra("extra_prefs_set_next_text", nextBtnText)
    }
    startActivity(intent)
}

/**
 * 跳转到使用访问控制设置界面
 */
fun Context.toUsageAccessSetting(
    showBackBtn: Boolean = true,
    backText: String = getString(R.string.com_jackiepenghe_back),
    showNextBtn: Boolean = false,
    nextBtnText: String = getString(R.string.com_jackiepenghe_finish)
) {
    val intent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
    intent.putExtra("extra_prefs_show_button_bar", showBackBtn)
    intent.putExtra("extra_prefs_set_back_text", backText)
    if (showNextBtn) {
        intent.putExtra("extra_prefs_set_next_text", nextBtnText)
    }
    startActivity(intent)
}

/**
 * 跳转到安全设置界面
 */
fun Context.toSecuritySetting(
    showBackBtn: Boolean = true,
    backText: String = getString(R.string.com_jackiepenghe_back),
    showNextBtn: Boolean = false,
    nextBtnText: String = getString(R.string.com_jackiepenghe_finish)
) {
    val intent = Intent(Settings.ACTION_SECURITY_SETTINGS)
    intent.putExtra("extra_prefs_show_button_bar", showBackBtn)
    intent.putExtra("extra_prefs_set_back_text", backText)
    if (showNextBtn) {
        intent.putExtra("extra_prefs_set_next_text", nextBtnText)
    }
    startActivity(intent)
}

/**
 * 跳转到指定包名APP的详细信息界面
 */
fun Context.toAppDetailsSetting(packageName: String = getPackageName()) {
    val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
    intent.setData(Uri.fromParts("package", packageName, null))
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    startActivity(intent)
}

/**
 * 跳转到目标界面
 */
fun <T : Activity> Context.startActivity(target: Class<T>) {
    val intent = if (this is Activity) {
        Intent(this, target)
    } else {
        val cache = Intent(this, target)
        cache.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        cache
    }
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
    this.startActivity(intent)
}

fun <T : Activity> Context.intentForNewTask(target: Class<T>): Intent {
    return if (this is Activity) {
        Intent(this, target)
    } else {
        val cache = Intent(this, target)
        cache.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        cache
    }
}

/**
 * 判断当前APP是否是默认启动器
 */
fun Context.isDefaultLauncher(): Boolean {
    val intent = Intent(Intent.ACTION_MAIN)
    intent.addCategory(Intent.CATEGORY_HOME)
    intent.addCategory(Intent.CATEGORY_DEFAULT)
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)

    val resolveInfo = packageManager?.resolveActivity(intent, 0) ?: return false
    val activityInfo = resolveInfo.activityInfo ?: return false
    return activityInfo.packageName == packageName
}

/**
 * 判断当前APP是否处于前台
 */
fun Context.isAppForeground(packageName: String = getPackageName()): Boolean {
    val activityManager =
        ContextCompat.getSystemService(this, ActivityManager::class.java) ?: return false
    val appProcesses = activityManager.runningAppProcesses ?: return false
    for (appProcess in appProcesses) {
        if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName == packageName) {
            return true
        }
    }
    return false
}